package com.cx330;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

public class Example60 {
    @Test
    public void test1() {
        long start = System.currentTimeMillis();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 100000; i++) {
            list.add(i - 1, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("ArrayList末尾位置添加时间：" + (end - start)); // 7
    }

    @Test
    public void test2() {
        long start = System.currentTimeMillis();
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 1; i <= 100000; i++) {
            list.add(i -1, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("LinkedList末尾位置添加时间：" + (end - start)); // 7
    }

    @Test
    public void test3() {
        long start = System.currentTimeMillis();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1); // 这里先添加1个，保证list.size()不为0
        Random random = new Random();
        for (int i = 1; i <= 100000; i++) {
            int index = random.nextInt(list.size() + 1);
            list.add(index, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("ArrayList任意位置添加时间：" + (end - start)); // 211
    }

    @Test
    public void test4() {
        long start = System.currentTimeMillis();
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1); // 这里先添加1个，保证list.size()个数不为0
        Random random = new Random();
        for (int i = 1; i <= 100000; i++) {
            int index = random.nextInt(list.size() + 1);
            list.add(index, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("LinkedList任意位置添加时间：" + (end - start)); // 14738
    }
}
